#encoding=utf-8

from openpyxl import load_workbook
import re
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import sys

reload(sys)
sys.setdefaultencoding( "utf-8" )
mail_host = "spam.avita.com.tw"  # 设置服务器
mail_user = "tao_lu"  # 用户名
mail_pass = "P2ss.w0rd"  # 口令
sender = 'tao_lu@avita.com.tw'



wb = load_workbook(ur'P:\製造部\每时间段产出日报.xlsx',data_only=True)

ws = wb[u'Sheet3']

emailRegex = r"[-_\w\.]{0,64}@[-\w]{1,63}\.*[-\w]{1,63}\.*[-\w]*"
emailAddressPattern = re.compile(emailRegex)
time = ws['A4'].value

for row in ws.rows:
    if row[12].value == None:
        continue
    emailAddress = emailAddressPattern.findall(row[12].value)
    if emailAddress != []:
        htmlTable_check = ''
        receivers_check = ['tao_lu@avita.com.tw']
        for item in emailAddress:
            receivers_check.append(item)


        for item in row[2:-1]:
            

            htmlTable_check += u'''<td class="tg-baqh">''' +str(item.value) + u'''</td>'''

        htmlContent_check = u'''<html>
                    <head>
    
                        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
                        <meta http-equiv="content-type" content="text/html;charset=utf-8">
    
    
                <title>TPM</title>
    
                <style type="text/css">
                .tg  {border-collapse:collapse;border-spacing:0;border-color:#aabcfe;}
                .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#669;background-color:#e8edff;}
                .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#039;background-color:#b9c9fe;}
                .tg .tg-baqh{text-align:center;vertical-align:top}
                .tg .tg-amwm{font-weight:bold;text-align:center;vertical-align:top}
                </style>
                </head>
                <body>
                <table class="tg">
                  <tr>
                    <th class="tg-amwm">时间</th>
                    <th class="tg-amwm">機種</th>
                    <th class="tg-amwm">批量</th>
                    <th class="tg-amwm">当日排程</th>
                    <th class="tg-amwm">时间段排程</th>
                    <th class="tg-amwm">实际产出</th>
                    <th class="tg-amwm">时间段差异数</th>
                    <th class="tg-amwm">不良数</th>
                    <th class="tg-amwm">异常状况说明</th>
                    <th class="tg-amwm">责任单位</th>
                    <th class="tg-amwm">责任人</th>
                  </tr><tr><td class="tg-baqh">'''+ time +  u'''</td>
                  ''' + htmlTable_check + u'''</tr></table></body>'''
        message = MIMEMultipart()
        message['From'] = Header("AutoMail")
        message['To'] =  Header("PersonLiable")
        subject = '排程达成状况'
        message['Subject'] = Header(subject, 'utf-8')
        message.attach(MIMEText(htmlContent_check, 'html', 'utf-8'))

        # 构造附件1，传送当前目录下的 test.txt 文件

        #att1 = MIMEText(open('meal.xls', 'rb').read(), 'base64', 'utf-8')

        #att1["Content-Type"] = 'application/octet-stream'

        # 这里的filename可以任意写，写什么名字，邮件中显示什么名字

        #att1["Content-Disposition"] = 'attachment; filename="餐厅刷卡记录.xls"'

        #message.attach(att1)

        try:
            smtpObj = smtplib.SMTP()
            smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号

            smtpObj.login(mail_user, mail_pass)
            smtpObj.sendmail(sender, receivers_check, message.as_string())
            print "邮件发送成功"
        except smtplib.SMTPException:
            print "Error: 无法发送邮件"
        except Exception:
            pass

